package com.zzh;

import com.zzh.framework.ComponentScan;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.context.annotation.Bean;

import java.beans.PropertyEditor;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName AppConfig
 * @date 2021/4/5 20:03
 * @Version 1.0
 * @Description //模拟spring
 * @USER root
 */
@ComponentScan("com.zzh.service")
public class AppConfig {

    @Bean
    public CustomEditorConfigurer customEditorConfigurer() {
        CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
        Map<Class<?>, Class<? extends PropertyEditor>> propertyEditorMap = new HashMap<>();

        // 表示StringToUserPropertyEditor可以将String转化成User类型，在Spring源码中，如果发现当前对象是String，而需要的类型是User，就会使用该PropertyEditor来做类型转化
        propertyEditorMap.put(User.class, StringToUserPropertyEditor.class);
        customEditorConfigurer.setCustomEditors(propertyEditorMap);
        return customEditorConfigurer;
    }
}
